//
//  MQVideoMainViewController.m
//  MQ
//
//  Created by lanou on 15/10/26.
//  Copyright © 2015年 徐永祥. All rights reserved.
//

#import "MQVideoMainViewController.h"
#import "MQVideoEntertainmentViewController.h"
#import "MQVideoSocietyViewController.h"
#import "LewPopupViewAnimationDrop.h"
#import "MQPopupView.h"
#import <MediaPlayer/MediaPlayer.h>
#import "MQChangeSkin.h"
@interface MQVideoMainViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong)MQVideoEntertainmentViewController * videoEnterVC;
@property (nonatomic, strong)MQVideoSocietyViewController * videoSocietyVC;
@property (nonatomic, strong)UIScrollView * scrollView;
@property (nonatomic, strong)UIButton * happyButton;
@property (nonatomic, strong)UIButton * societyButton;
@property (nonatomic, strong)UIImageView * backGroundView;
@property (nonatomic, strong)UIImageView * headView;

@end

@implementation MQVideoMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"daohanglan"] forBarMetrics:UIBarMetricsDefault];
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd) target:self action:@selector(setting:)];
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor whiteColor]];
    self.navigationController.navigationBarHidden = NO;
    NSUserDefaults * user = [NSUserDefaults standardUserDefaults];
    NSString *picStr = [user valueForKey:@"picStr"];
    UIImageView *BigImageView = [[UIImageView alloc]initWithFrame:self.view.bounds];
    [BigImageView setImage:[UIImage imageNamed:picStr]];
    BigImageView.userInteractionEnabled = YES;
    [self.view addSubview:BigImageView];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"一键换肤" style:(UIBarButtonItemStylePlain) target:self action:@selector(changeImage)];
     [self.navigationItem.leftBarButtonItem setTintColor:[UIColor whiteColor]];
    NSArray * array = @[@"开心一刻",@"社会百态"];
    _segmentedControl = [[UISegmentedControl alloc]initWithItems:array];
    _segmentedControl.frame = CGRectMake(screen_width/2-80, 5, 160, 30);
    _segmentedControl.tintColor = [UIColor whiteColor];
    _segmentedControl.selectedSegmentIndex = 0;
     [_segmentedControl addTarget:self action:@selector(changeView:) forControlEvents:(UIControlEventValueChanged)];
    [self.navigationController.navigationBar addSubview:_segmentedControl];
   
    
    _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0,0, screen_width, screen_height)];
    _scrollView.backgroundColor = [UIColor clearColor];
    
    [BigImageView addSubview:_scrollView];
//    添加开心一刻的视频
    _videoEnterVC = [[MQVideoEntertainmentViewController alloc]init];
    _videoEnterVC.view.frame =CGRectMake(0,0, screen_width, screen_height);
       _videoEnterVC.mainVC = self;
    _videoEnterVC.view.backgroundColor = [UIColor clearColor];
       [self.scrollView addSubview:_videoEnterVC.view];
//    添加社会百态的视频
    _videoSocietyVC = [[MQVideoSocietyViewController alloc]init];
    _videoSocietyVC.view.frame = CGRectMake(screen_width,0 , screen_width, screen_height);
    _videoSocietyVC.view.backgroundColor = [UIColor clearColor];
    [self.scrollView addSubview:_videoSocietyVC.view];
//
//    scroView的滑动范围
    self.scrollView.contentSize =CGSizeMake(2*screen_width, 0);
    self.scrollView.pagingEnabled = YES;
//    设置代理
    self.scrollView.delegate = self;
    
   }


-(void)changeImage
{
    MQChangeSkin *view = [MQChangeSkin defaultPopupView];
    view.parentVC = self;
    [self lew_presentPopupView:view animation:[LewPopupViewAnimationDrop new] dismissed:^{
        NSLog(@"动画结束");
        
    }];

}

-(void)changeView:(UISegmentedControl *)segmentControl
{
    self.scrollView.contentOffset = CGPointMake(_segmentedControl.selectedSegmentIndex *screen_width, 0);
    if (_segmentedControl.selectedSegmentIndex==1) {
        [_videoEnterVC.playVC.view removeFromSuperview];
        [_videoEnterVC.playVC.player pause];
    }
}

-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{

    _segmentedControl.selectedSegmentIndex = self.scrollView.contentOffset.x/screen_width;
    [[NSNotificationCenter defaultCenter]postNotificationName:@"xxx" object:nil userInfo:nil];
    if (_segmentedControl.selectedSegmentIndex==1) {
        [_videoEnterVC.playVC.view removeFromSuperview];
        [_videoEnterVC.playVC.player pause];
    }
}



//右上角设置按钮
- (void)setting:(UIBarButtonItem *)barButton
{
    MQPopupView *view = [MQPopupView defaultPopupView];
    view.parentVC = self;
    [self lew_presentPopupView:view animation:[LewPopupViewAnimationDrop new] dismissed:^{
        NSLog(@"动画结束");
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
